package timing.ukulele.seal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.seal.persistent.Seal;
import timing.ukulele.seal.service.SealService;
import timing.ukulele.seal.vo.SealVO;
import timing.ukulele.web.controller.BaseController;

import java.util.Date;

@RestController
@RequestMapping("/seal")
@Slf4j
public class SealController extends BaseController {
    private final SealService service;

    public SealController(SealService service) {
        this.service = service;
    }

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysDept
     */
    @GetMapping("/{id}")
    public ResponseData<SealVO> get(@PathVariable(value = "id")Long id) {
        if (id == null || id <= 0)
            return paraErrorResponse();
        Seal seal = service.getById(id);
        if (seal != null) {
            SealVO vo = new SealVO();
            BeanUtils.copyProperties(seal, vo);
            return successResponse(vo);
        }
        return successResponse();
    }

    /**
     * 添加
     *
     * @param vo 实体
     * @return success/false
     */
    @PostMapping()
    public ResponseData<Boolean> add(@RequestHeader("X-USER") String currentUser,@RequestBody SealVO vo) {
        if (vo == null || vo.getId() != null)
            return paraErrorResponse();
        Seal seal = new Seal();
        BeanUtils.copyProperties(vo, seal);
        seal.setCreateBy(currentUser);
        return successResponse(this.service.save(seal));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public ResponseData<Boolean> delete(@RequestHeader("X-USER") String currentUser,@PathVariable(value = "id") Long id) {
        if (id == null || id <= 0)
            return paraErrorResponse();
        try {
            this.service.lambdaUpdate().set(Seal::getDeleted, Boolean.TRUE)
                    .set(Seal::getUpdateBy, currentUser).set(Seal::getUpdateTime, new Date())
                    .eq(Seal::getId, id).update();
            return successResponse();
        } catch (Exception e) {
            log.error("印章删除失败," + e.getMessage());
            return successResponse();
        }
    }

    /**
     * 编辑
     *
     * @param vo 实体
     * @return success/false
     */
    @PutMapping()
    public ResponseData<Boolean> edit(@RequestHeader("X-USER") String currentUser,@RequestBody SealVO vo) {
        if (vo == null || vo.getId() == null)
            return paraErrorResponse();
        Seal seal = new Seal();
        vo.setSealFile(null);
        BeanUtils.copyProperties(vo, seal);
        seal.setUpdateBy(currentUser);
        return successResponse(this.service.saveOrUpdate(seal));
    }

    @GetMapping("/page/{current}/{size}")
    public ResponseData<IPage<SealVO>> getPage(@PathVariable(name = "current") int current,
                                               @PathVariable(name = "size") int size,
                                               @RequestParam(value = "name", required = false) String name,
                                               @RequestParam(value = "type", required = false) Integer type) {
        if (size == 0) size = 10;
        if (current == 0) current = 1;
        IPage<SealVO> page = this.service.getPage(name, type, current, size);
        return successResponse(page);
    }

    /**
     * 上传印章图片，原图先保存，后通过rabbitmq发消息进行异步抠图
     *
     * @param id   印章数据id
     * @param file 印章图片
     * @return 上传结果
     */
    @PostMapping("/sealFile/{id}")
    public ResponseData<String> setSealFile(@RequestHeader("X-USER") String currentUser,@PathVariable("id") Long id, @RequestBody MultipartFile file) {
        Seal seal = this.service.getById(id);
        if (seal == null)
            return failResponse(null, "印章数据不存在");
        if (file == null || !StringUtils.hasText(file.getOriginalFilename()))
            return paraErrorResponse("印章文件必传");
        try {
            seal.setUpdateBy(currentUser);
            this.service.setSealFile(seal, file);
            return successResponse();
        } catch (Exception e) {
            log.error("印章文件保存失败，" + e.getMessage());
        }
        return failResponse();
    }

}
